package fr.gestschool.login.controler;

import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;


import fr.gestschool.login.User.User;
import fr.gestschool.login.User.UserDAO;
import fr.gestschool.login.bean.LoginBean;
import fr.gestschool.login.view.LoginView;
import fr.gestschool.util.Utils;


public class LoginControler {
		LoginBean bean;
		LoginView view;
		
		public LoginControler() {
			
			this.bean = new LoginBean();
			this.view = new LoginView();
		}
		
		
		
		public LoginBean getBean() {
			return bean;
		}
		public void setBean(LoginBean bean) {
			this.bean = bean;
		}
		public LoginView getView() {
			return view;
		}
		public void setView(LoginView view) {
			this.view = view;
		}
		
		
		public String validate(){
			HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

			InitialContext ctx;
			try {
				ctx = Utils.getInitialContext();
				UserDAO dao = (UserDAO) ctx.lookup("UserDAOBean/remote");
				User user = new User();
				user.setLogin((String)view.getLogin().getValue());
				user.setPassword((String)view.getMdp().getValue());
				user.setSkin("1");
				dao.addUser(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


			if(view.getLogin().getValue().equals("julien")&&view.getMdp().getValue().equals("julien")){
			request.setAttribute("skin","2");
				return "loginOK";
			}
			else {
				request.setAttribute("skin","1");
			return "loginError";
		
	}

}

}
